home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / file / managers / mc-3.2 / mc-3 / mc-3.2.1 / slang / slgetkey.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-17  |  2.0 KB  |  95 lines

  1. /* Copyright (c) 1992, 1995 John E. Davis
  2.  * All rights reserved.
  3.  * 
  4.  * You may distribute under the terms of either the GNU General Public
  5.  * License or the Perl Artistic License.
  6.  */
  7. #include <config.h>
  8. #include <stdio.h>
  9. #include "slang.h"
  10. #include "_slang.h"
  11.  
  12. int SLang_Input_Buffer_Len = 0;
  13. unsigned char SLang_Input_Buffer [MAX_INPUT_BUFFER_LEN];
  14.  
  15. int SLang_Abort_Char = 7;
  16. volatile int SLKeyBoard_Quit = 0;
  17. int SLang_Ignore_User_Abort = 0;
  18.  
  19.  
  20. unsigned int SLang_getkey (void)
  21. {
  22.    int imax;
  23.    unsigned int ch;
  24.    
  25.    if (SLang_Input_Buffer_Len)
  26.      {
  27.     ch = (unsigned int) *SLang_Input_Buffer;
  28.     SLang_Input_Buffer_Len--;
  29.     imax = SLang_Input_Buffer_Len;
  30.    
  31.     MEMCPY ((char *) SLang_Input_Buffer, 
  32.         (char *) (SLang_Input_Buffer + 1), imax);
  33.      }
  34.    else
  35.      {
  36.     ch = SLsys_getkey ();
  37.      }    
  38.    return(ch);
  39. }
  40.  
  41.  
  42. void SLang_ungetkey_string (unsigned char *s, int n)
  43. {
  44.    register unsigned char *bmax, *b, *b1;
  45.    if (SLang_Input_Buffer_Len > MAX_INPUT_BUFFER_LEN - 3 - n) return;
  46.  
  47.    b = SLang_Input_Buffer;
  48.    bmax = b + (SLang_Input_Buffer_Len - 1);
  49.    b1 = bmax + n;
  50.    while (bmax >= b) *b1-- = *bmax--;
  51.    bmax = b + n;
  52.    while (b < bmax) *b++ = *s++;
  53.    SLang_Input_Buffer_Len += n;
  54. }
  55.  
  56. void SLang_buffer_keystring (unsigned char *s, int n)
  57. {
  58.  
  59.    if (n + SLang_Input_Buffer_Len > MAX_INPUT_BUFFER_LEN - 3) return;
  60.    
  61.    MEMCPY ((char *) SLang_Input_Buffer + SLang_Input_Buffer_Len, 
  62.        (char *) s, n);
  63.    SLang_Input_Buffer_Len += n;
  64. }
  65.  
  66. void SLang_ungetkey (unsigned char ch)
  67. {
  68.    SLang_ungetkey_string(&ch, 1);
  69. }
  70.  
  71. int SLang_input_pending (int tsecs)
  72. {
  73.    int n;
  74.    unsigned char c;
  75.    if (SLang_Input_Buffer_Len) return SLang_Input_Buffer_Len;
  76.    
  77.    n = SLsys_input_pending (tsecs);
  78.    if (n)
  79.      {
  80.     c = (unsigned char) SLang_getkey ();
  81.     SLang_ungetkey_string (&c, 1);
  82.      }
  83.    return n;
  84. }
  85.  
  86. void SLang_flush_input (void)
  87. {
  88.    int quit = SLKeyBoard_Quit;
  89.    
  90.    SLang_Input_Buffer_Len = 0;
  91.    SLKeyBoard_Quit = 0;   
  92.    while (SLsys_input_pending (0) > 0) (void) SLsys_getkey ();
  93.    SLKeyBoard_Quit = quit;
  94. }
  95.